/*index基础操作*/
$(function() {
	$('#sidebar').move(function() {
		$(this).parent().scrollTop($(this).parent().scrollTop() - this.pageYc);
	});

	$('#tab-nav').move(function() {
		$(this).scrollLeft($(this).scrollLeft() - this.pageXc);
	});
	$('#tab-nav').mousewheel(function() {
		var i = (this.down ? 1 : -1);
		$(this).stop().animate({
			scrollLeft: $(this).scrollLeft() + 100 * i
		}, 100);
	});
	window.tabAnchor = function(dom) {
		$("#tab-nav").stop().animate({
			scrollLeft: $(dom).get(0).offsetLeft
		}, 500);
	};
	$('.fold-nav').click(function() {
		$('.wrap-main').toggleClass('short-side')
	});
});

$(function() {
	/*主导航切换*/
	$('.nav-unselect')
		.on('mouseenter', function() {
			$(this).find('.nav-child').addClass('layui-show')
		})
		.on('mouseleave', function() {
			$(this).find('.nav-child').removeClass('layui-show')
		});
	/*sidebar/tab切换*/
	$('#sidebar').on('click', '.sidebar-item', function() {
		var $sidebarItem = $('#sidebar .sidebar-item');
		var $this = $(this);
		$this.toggleClass('sidebar-itemed');
		if($this.next('.sidebar-child').length) {
			$this.siblings('.sidebar-item').removeClass('sidebar-itemed');
		} else {
			$sidebarItem.removeClass('sidebar-active');
			$this.addClass('sidebar-active');
		}
	});
	/*sidebar/tab切换事件功能*/
	var $tabNavW = $('#tab-nav'),
		$sidebarW = $('#sidebar');
	$('#sidebar').on('click', '.sidebar-item', function() {
		var $this = $(this),
			thisHref = $this.attr('href');
		if($this.attr('target') == 'xq') {
			var $thisTabNav = $tabNavW.find('[href="' + thisHref + '"]');
			if($thisTabNav.length == 0) {
				var $thisTabNav = $('<a class="tab-nav-item" href="' + thisHref + '" target="xq">' + $this.find('.text').text() + '<i class="tab-close layui-icon"></i></a>');
				$tabNavW.append($thisTabNav);
			};
			$thisTabNav.addClass('active').siblings().removeClass('active');
			tabAnchor($thisTabNav);
		}
	});
	$('#tab-nav').on('click', '.tab-nav-item', function() {
		var $sidebarItem = $('#sidebar .sidebar-item');
		var $this = $(this),
			thisHref = $this.attr('href');
		$this.addClass('active').siblings().removeClass('active');

		var $thisSidebarItem = $sidebarW.find('[href="' + thisHref + '"]');
		if($thisSidebarItem.length != 0) {
			$sidebarItem.removeClass('sidebar-active');
			$thisSidebarItem.addClass('sidebar-active');
			$thisSidebarItem.siblings().removeClass('sidebar-itemed');
			$thisSidebarItem.parents('.sidebar-child').prev().addClass('sidebar-itemed').siblings().removeClass('sidebar-itemed');
		};
	});
	//打开额外tab
	window.tabOpenExtra = function(name, url) {
		var $thisTabNav = $tabNavW.find('[href="' + url + '"]');
		if($thisTabNav.length == 0) {
			var $thisTabNav = $('<a class="tab-nav-item" href="' + url + '" target="xq"><span>' + name + '</span><i class="tab-close layui-icon"></i></a>');
			$tabNavW.append($thisTabNav);
		};
		$thisTabNav.find('span').trigger("click");
	};

	$('#tab-nav').on('click', '.tab-close', function(event) {
		event.preventDefault();
		var $this = $(this).parent();
		if($this.is('.active')) {
			var $thisNext = $this.next().length ? $this.next() : $this.prev();
			var _span = $('<span/>');
			$thisNext.append(_span);
			_span.trigger('click');
			_span.remove();
		}
		$this.remove();
	});

	/*时间显示*/
	var nowDom = document.getElementById('now'),
		nowdate = nowDom.querySelector(".date"),
		nowtime = nowDom.querySelector(".time");
	setInterval(function() {
		var now = new Date();
		nowdate.innerText = now.getFullYear() + '年' + (now.getMonth() + 1) + '月' + now.getDate() + '日';
		nowtime.innerText = now.getHours() + ':' + now.getMinutes() + ':' + now.getSeconds();
	}, 1000);
});